package com.chengweiyi.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.jnlp.PersistenceService;
import javax.sql.DataSource;

/**
 * @Author xiao hu die
 * @Date 2022/5/7 23:09
 * @Version 1.0
 */
@Configuration
public class SecurityConfigurationTest extends WebSecurityConfigurerAdapter {
    /**注入自定义实现的userDetailService*/
    @Autowired
    private UserDetailsService userDetailsService;
    /**注入数据源*/
    @Autowired
    private DataSource dataSource;
    /**配置对象*/
    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        return jdbcTokenRepository;
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);
      /*  BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123");
        System.out.println("_________________"+encode);
        auth.inMemoryAuthentication().withUser("chengweiyi").password(encode).roles("admin");*/
        auth.userDetailsService(userDetailsService).passwordEncoder(create());
    }
  @Bean
    PasswordEncoder create(){
        return new BCryptPasswordEncoder();
  }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /**配置退出成功之后的访问路径*/
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/logout").permitAll();
        /**配置没有权限控制之后的页面跳转*/
       http.exceptionHandling().accessDeniedPage("/unauth.html");
       http.formLogin()
               .loginPage("/login.html") //登录界面设置
               .loginProcessingUrl("/user/login") //登录访问路径
               .defaultSuccessUrl("/success.html").permitAll() //登录成功跳转路径
               .and().authorizeRequests()
               .antMatchers("/","/test/hello","/user/login").permitAll() //设置那些路径可以直接访问，不需要认证
               //设置权限访问(hasAuthority只适合单个权限控制)
               .antMatchers("/test/index").hasAuthority("role")
               //设置多个权限访问
               //.antMatchers("/test/index").hasAnyAuthority("role,admin")
               //hasRole方法,ROLE_sale 基于角色与权限不同
               //.antMatchers("/test/index").hasRole("sale")
               //具有多个角色 具有其中之一的角色即可
               //.antMatchers("/test/index").hasAnyRole("sale")
               .anyRequest().authenticated()
               .and().rememberMe().tokenRepository(persistentTokenRepository())
               .tokenValiditySeconds(60)
               .userDetailsService(userDetailsService)
               .and().csrf().disable(); //关闭csrf防护
     }
}
